<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
<!-- 导航栏 -->
<div class="nav">
    <!-- logo -->
    <img src="img/cat.jpg" alt="">
    <!-- 标题 -->
    <span class="title">Java 在线OJ 系统</span>
    <!-- 空白的占位符 -->
    <span class="spacer"></span>
    <!-- 右侧的几个链接 -->
    <a class="navbar-brand font-weight-bold" href="index.html">主页</a>
    <a class="navbar-brand font-weight-bold" href="javascript:void(0);"  onclick=logout()>注销</a>
</div>

<!-- 登录页面的页面容器, 为了和其他页面样式区分开, 使用不同的类名 -->
<div class="login-container" >
    <div class="login-dialog" style="height: 380px">
        <!-- 标题 -->
        <h3>重 置 密 码</h3>

        <!-- 输入用户名 -->
        <div class="row">
            <span>用户名</span>
            <input type="username" readonly="readonly" id="username" style="font-size: 15px">
        </div>

        <!-- 输入密码 -->
        <div class="row">
            <span>密码</span>
            <input type="password" id="password" name="password">
        </div>

        <div class="row">
            <span>确认密码</span>
            <input type="password" id="required" name="password">
        </div>



        <div class="row submit-row">
            <input id="submit" type="submit" onclick=update() value="确认修改">
        </div>



    </div>
</div>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>


    $.ajax({
        url:"usermanage/emailUser"+location.search,
        type: "GET",
        success:function (data) {
            if(data.state===6080){
                alert("您的邮箱未注册，无法重置密码!");
                location.href="login.html";
                return;
            }
             let username = document.querySelector("#username");
             username.value=data.data.username;
        }
    })

    let Lpassword = document.querySelector("#password");
    let Lrequired = document.querySelector("#required");

    function update() {
         if(Lpassword.value===""|| Lrequired.value===""){
             alert("密码或确认密码不能为空，请重新输入!");
             return;
         }

         if(Lpassword.value!==Lrequired.value){
             alert("两次输入密码不一致，请重新输入!");
             return;
         }

         let body={
             username:document.querySelector("#username").value,
             password:Lpassword.value
         }

         $.ajax({
             url:"state/updatePassword",
             type:"post",
             data:JSON.stringify(body),
             contentType:"application/json",
             success:function (data) {
                 alert(data.message);
             },error:function () {
                  alert("服务器异常，访问接口失败!")
             }
         })
    }


    function logout() {
        $.ajax({
            url:"state/logout",
            type:"POST",
            success:function (data,status) {
                alert(data.message);
                if(data.state===5002){
                    location.href="login.html";
                }
            }
        })
    }



</script>


</body>
</html>